"""
工作台相关路由
"""

from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session

from database import get_db
from models.common import StandardResponse
from services.auth_service import get_current_user
from services.workspace_service import WorkspaceService

router = APIRouter(prefix="/workspace", tags=["工作台"])

@router.get("/apps", response_model=StandardResponse, summary="获取工作台应用")
async def get_workspace_apps(
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_db)
) -> StandardResponse:
    """
    获取用户工作台应用
    
    返回用户个人常用应用和快速访问入口
    """
    try:
        workspace_service = WorkspaceService(db)
        workspace_data = await workspace_service.get_workspace_apps(current_user["id"])
        
        return StandardResponse(
            success=True,
            message="获取工作台应用成功",
            data=workspace_data
        )
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"获取工作台应用失败: {str(e)}"
        )

@router.get("/statistics", response_model=StandardResponse, summary="获取工作台统计")
async def get_workspace_statistics(
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_db)
) -> StandardResponse:
    """
    获取用户工作台统计数据
    """
    try:
        workspace_service = WorkspaceService(db)
        statistics = await workspace_service.get_user_statistics(current_user["id"])
        
        return StandardResponse(
            success=True,
            message="获取工作台统计成功",
            data=statistics
        )
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"获取工作台统计失败: {str(e)}"
        )

@router.get("/recent-activities", response_model=StandardResponse, summary="获取最近活动")
async def get_recent_activities(
    limit: int = 10,
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_db)
) -> StandardResponse:
    """
    获取用户最近活动
    
    - **limit**: 返回数量限制
    """
    try:
        workspace_service = WorkspaceService(db)
        activities = await workspace_service.get_recent_activities(
            current_user["id"], 
            limit
        )
        
        return StandardResponse(
            success=True,
            message="获取最近活动成功",
            data=activities
        )
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"获取最近活动失败: {str(e)}"
        )